package com.lemieripetizioni.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lemieripetizioni.R;
import com.lemieripetizioni.datamodel.Professore;
import com.lemieripetizioni.dbadapter.ImageLoader;

public class ProfessoreListAdapterWithCache extends ArrayAdapter<Professore> implements OnItemClickListener {

	private Activity activity;
	private List<Professore> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	int resourceId;

	public ProfessoreListAdapterWithCache(Context ctx, int resourceId, List<Professore> objects, Activity form1) {
		super(ctx, resourceId, objects);
		activity = form1;
		data = objects;
		// inflater = (LayoutInflater)
		// activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater = LayoutInflater.from(ctx);
		imageLoader = new ImageLoader(activity.getApplicationContext());
		this.resourceId = resourceId;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(resourceId, null);

		Professore prof = (Professore) getItem(position);

		TextView nome = (TextView) vi.findViewById(R.id.nome);
		nome.setText(prof.getNome());

		TextView cognome = (TextView) vi.findViewById(R.id.cognome);
		cognome.setText(prof.getCognome());

		TextView data_nascita = (TextView) vi.findViewById(R.id.data_nascita);
		data_nascita.setText(prof.getDataNascita());

		TextView materia_insegnata = (TextView) vi.findViewById(R.id.materia_insegnata);
		materia_insegnata.setText(prof.getMateriaInsegnata());

		ImageView image = (ImageView) vi.findViewById(R.id.url_foto);
		imageLoader.DisplayImage(prof.getUrlFoto(), image);

		image.setFocusable(false);
		nome.setFocusable(false);
		cognome.setFocusable(false);
		data_nascita.setFocusable(false);
		materia_insegnata.setFocusable(false);

		return vi;
	}

	public int getCount() {
		return data.size();
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Toast.makeText(getContext(), "Postion:" + position, Toast.LENGTH_SHORT).show();
	}
}